package org.cwnuit.course.service;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.cwnuit.course.dao.CourseDAO;
import org.cwnuit.course.domain.Course;
import org.cwnuit.course.domain.Coursebox;

/**
 * 排课信息表Service
 * @author john
 */
public class CourseService {
	private CourseDAO courseDAO =  new CourseDAO();
	/**
	 * 通过学科ID获取学科信息
	 * @param objectid
	 */
	public Course getCourse(Integer objectid){
		return courseDAO.getCourse(objectid);
	}
	
	/**
	 * 获取用所有学科信息
	 * @return
	 */
	public List<Course> listCourse(){
		return courseDAO.listCourse();
	}

	/**
	 * 添加或修改学科信息
	 * @param request
	 */
	public boolean saveCourse(HttpServletRequest request) {
		String objectidStr = request.getParameter("objectid")==""?"0":request.getParameter("objectid");
		int objectid = Integer.parseInt(objectidStr);
		Coursebox coursebox = (Coursebox)request.getSession().getAttribute("coursebox");
		int courseid = coursebox.getCourseid();
		System.out.println(courseid);
		String objectname = request.getParameter("objectname");
		String remarks = request.getParameter("remarks");
		
		Course course = new Course();
		course.setObjectid(objectid);
		course.setCourseid(courseid);
		course.setObjectname(objectname);
		course.setRemarks(remarks);
		try{
			if(objectid != 0){
				courseDAO.updateCourse(course);
			}else{
				courseDAO.insertCourse(course);
			}
			return true;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}
	
	/**
	 * 删除学科信息
	 * @param userid
	 */
	public boolean deleteCourse(Integer objectid){
		try{
			Course course = new Course();
			course.setObjectid(objectid);
			System.out.println(course.getObjectid());
			courseDAO.deleteCourse(course);
			return true;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
		
	}
}
